package com.hl.springboot4.controller;

import com.github.pagehelper.PageInfo;
import com.hl.springboot4.entity.Article;
import com.hl.springboot4.service.ArticleService;
import com.hl.springboot4.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName ArticleController
 * @Description TODO
 * @Author libowen
 * @Date 2025年03月03日 10:28
 * @Version 1.0
 */
@RestController
@RequestMapping("/article")
//@CrossOrigin   // 允许跨域访问
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    //新增
    //@RequestMapping(value="/insert",method = RequestMethod.POST)  // 默认支持 get  post  put  delete options  head
//    @PostMapping("/insert")
    @PostMapping
    public R insert( @RequestBody Article article){
        int result = articleService.insert(article);
        if(result>0){
            return R.ok("新增成功");
        }else{
            return R.fail("新增失败");
        }
    }

    //修改
//    @RequestMapping("/update")
    @PutMapping
    public R update(@RequestBody Article article){
        int result = articleService.update(article);
        if(result>0){
            return R.ok("修改成功");
        }else {
            return R.fail("修改失败");
        }
    }

    //删除
//    @RequestMapping("/delete")  // localhost:8080/article/delete?id=1
    @DeleteMapping("/{id}")   // localhost:8080/article/1
    public R delete(@PathVariable("id") int id){
        int result = articleService.delete(id);
        if(result>0){
            return R.ok("删除成功");
        }else{
            return R.fail("删除失败");
        }
    }
    //根据id查询
//    @RequestMapping("/selectById")
    @GetMapping("/{id}")
    public R selectById(@PathVariable("id") int id){
        Article article = articleService.selectById(id);
        return R.ok(article);
    }
    //分页查询
    @RequestMapping("/selectAll")
    public R selectAll(@RequestParam(required = false,defaultValue = "1") int pageNum,
                       @RequestParam(required = false,defaultValue = "5") int pageSize,
                       Article article){
        PageInfo<Article> pageInfo = articleService.selectAll(pageNum, pageSize, article);
        return R.ok(pageInfo);
    }


}
